package Tests;

import static org.junit.Assert.*;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Test;

import Model.Role;

public class RoleTest {

	Role RoleTest =  new Role();
	

	@Test
	public void testGetAllRole() {
		ResultSet result = this.RoleTest.getAllRole();
		
		try{
		result.next();
		assertEquals(1, result.getInt("id_role"));
		assertEquals("test", result.getString("role_name"));
			
		result.next();
		assertEquals(2, result.getInt("id_role"));
		assertEquals("test2", result.getString("role_name"));
		}catch(SQLException e){
			e.printStackTrace();
		}
	}

	@Test
	public void testGetRoleById() {
		ResultSet result = this.RoleTest.getRoleById(1);
		try {
			result.next();
			assertEquals(result.getInt(1), 1);
			assertEquals(result.getString(2), "test");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Test
	public void testGetRoleByName() {
		ResultSet result = this.RoleTest.getRoleByName("test2");
		
		try {
			result.next();
			assertEquals(result.getInt(1), 2);
			assertEquals(result.getString(2), "test2");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

	@Test
	public void testInsertRole() {
			int id = this.RoleTest.insertRole("testJunit");
		
			try {
			ResultSet result = this.RoleTest.getRoleById(id);
			
			result.next();
			assertEquals(id, result.getInt("id_role"));
			assertEquals("testJunit", result.getString("role_name"));
			
			//wyczyszczenie wynikow
			this.RoleTest.deleteRoleByName("testJunit");
			
			} catch (SQLException e) {
				e.printStackTrace();
			}	
		
	}

	@Test
	public void testDeleteRoleById() {
		int id = this.RoleTest.insertRole("testJunitNaDelete");
		int idDelete=RoleTest.deleteRoleById(id);
		
		assertEquals(id, idDelete);
		
	}

	@Test
	public void testDeleteRoleByName() {
		int id = this.RoleTest.insertRole("testJunitDeleteByName");
		int idDeleted = this.RoleTest.deleteRoleByName("testJunitDeleteByName");
		
		assertEquals(id, idDeleted);
		
	}


}
